<?php

class DetailController extends BaseController
{

	public function init()
	{
		/* Initialize action controller here */
		//	$this->load_header .= '<link href="'.CSS_URL.'slide/slide.css" rel="stylesheet" type="text/css" />' ."\n";
		//  $this->load_header .= '<link href="'.CSS_URL.'jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />' ."\n";
		//   $this->load_header .= '<script type="text/javascript" src="'.JS_URL.'jquery.lightbox-0.5.js"></script>' ."\n";
		//	$this->load_header .= '<script type="text/javascript" src="'.JS_URL.'slide/scripts.js"></script>' ."\n";
		$this->load_header .= '<script type="text/javascript" src="'.JS_URL.'galleria-1.2.5.min.js"></script>' ."\n";
		$this->load_header .= '<link href="'.CSS_URL.'galleria.classic.css" rel="stylesheet" type="text/css" />' ."\n";


	}

	public function indexAction()
	{
		$request=$this->getRequest();
		$motor_id=$request->getParam('id',0);

		$mMotorSearch=new MotorSearch();
		if($motor_id>0){
			$sWhere='AND m.id='.$motor_id;
			$arrMotors = $mMotorSearch->getMotors($sWhere, '', 0, 1, false);
			if($arrMotors['total']>0){
				$row=$arrMotors['result'][0];

				// Get information of user
				$user_id=$row['user_id'];

				$mUser=new User();
				$arrUser=$mUser->getUserById($user_id);
				if(trim($arrUser['phone'])!='') $arrUser['phone']=' / '.$arrUser['phone'];
				$this->view->ARR_USER=$arrUser;
				$this->view->USERNAME=$arrUser['last_name'].' '.$arrUser['first_name'];

				$arrImages = array();

				if($row['image'] && file_exists(UPLOAD.'default/detail/'.$row['image']) ){

					$arrImages[]= UPLOAD_DIR.'default/detail/'.$row['image'];
				}

				if($row['image1'] && file_exists(UPLOAD.'default/detail/'.$row['image1']) ){
					$arrImages[]= UPLOAD_DIR.'default/detail/'.$row['image1'];
				}

				if($row['image2'] && file_exists(UPLOAD.'default/detail/'.$row['image2']) ){
					$arrImages[]= UPLOAD_DIR.'default/detail/'.$row['image2'];
				}
				$isNoImage=false;
				if (empty($arrImages)){

					$arrImages[]= IMAGE_URL.'no_img.png';
					$isNoImage=true;
				}

				$this->view->ARR_IMG = $arrImages;
				$this->view->IS_NO_IMAGE= $isNoImage;
					
				if ($row['manu_name']=="Khác" && $row['manu_khac']!="Thương hiệu xe của bạn là gì?") $row['manu_name'] = $row['manu_khac'];
				if ($row['model_name']=="Khác" && $row['model_khac']!="Model xe của bạn là gì? ") $row['model_name']= $row['model_khac'];
				$this->view->name = $row['manu_name'] . ' '.$row['model_name'];
				if ($this->view->name=="Khác Khác"&&($this->type==0)) $this->view->name = "Xe máy cũ";
				else if ($this->view->name=="Khác Khác"&&($this->type==1)) $this->view->name = "Ô tô cũ";
				$this->view->date = date('d/m/Y', strtotime($row['created_time']));
				if ($row['price']){
					$this->view->price = number_format ($row['price'], 0,'','.') . ' VNĐ';
				}else{
					$this->view->price = 'Thỏa thuận';
					//$this->view->price = $row['price_from'] .' Triệu - '. $row['price_to'].' Triệu';
				}
				$row ['kms'] = number_format ($row['kms'], 0,'','.');

				$this->view->DATA=$row;
				$this->view->city = $row['city_name'];
				$this->view->brand = $row['manu_name'];

				$this->view->H1 = str_replace (array('(city)','(price)','(brand)','(namebrand-namemodel)'),
				array($row['city_name'], $this->view->price, $row['manu_name'],$this->view->name), $this->view->H1);
				$this->page_title = str_replace (array('(city)','(price)','(brand)','(namebrand-namemodel)'),
				array($row['city_name'], $this->view->price, $row['manu_name'],$this->view->name), $this->page_title);
				$this->page_des = str_replace (array('(city)','(price)','(brand)','(namebrand-namemodel)'),
				array($row['city_name'], $this->view->price, $row['manu_name'],$this->view->name), $this->page_des);
				//$this->view->TITLE=($this->type==1)?'Bán ôto hiệu '.$row['manu_name']:'Bán xe máy hiệu '.$row['manu_name'];
			} else {
				$this->_helper->viewRenderer->setNoRender();
				echo '<center style="margin-top:20px">Không có kết quả phù hợp</center>';
			}
		}
	}
}

